package com.gzhryc.net_marketing.supplier.config.services;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import com.gzhryc.common.exceptions.LogicException;
import com.gzhryc.common.logging.Logger;
import com.gzhryc.jdbc.Conditions;
import com.gzhryc.jdbc.JdbcSession;
import com.gzhryc.jdbc.MultiDBTools;
import com.gzhryc.net_marketing.supplier.config.dao.SupplierLogisticsCompanyDao;
import com.gzhryc.net_marketing.supplier.config.dao.db.SupplierLogisticsCompany;
import com.gzhryc.net_marketing.supplier.config.dao.db.SupplierLogisticsCostConfig;
import com.gzhryc.net_marketing.supplier.config.dao.enums.SupplierLogisticsCompanyEnum;
import com.gzhryc.net_marketing.supplier.config.services.dto.SupplierLogisticsCompanySearch;
import com.gzhryc.shared_device.base.enums.ELogisticsAreaScope;

public class SupplierLogisticsCompanyService {

    static Logger log = Logger.getLogger(SupplierLogisticsCompanyService.class);

    public static SupplierLogisticsCompanyService self() {
        return MultiDBTools.getService(SupplierLogisticsCompanyService.class, true);
    }

    public static SupplierLogisticsCompanyService self(String jdbcKey) {
        return MultiDBTools.getService(jdbcKey, SupplierLogisticsCompanyService.class, true);
    }

    SupplierLogisticsCompanyDao dao;

    public SupplierLogisticsCompanyService(String jdbcKey) {
        dao = new SupplierLogisticsCompanyDao(jdbcKey);
    }

    public List<SupplierLogisticsCompany> findBySearch(SupplierLogisticsCompanySearch search, Integer pageNo, Integer pageLimit) {
        Conditions condition = dao.buildConditions(search);
        try {
            return dao.findByCondition(condition, pageNo, pageLimit);
        } catch (SQLException e) {
            log.error(e.getMessage(), e);
        }
        return null;
    }

    public Long countBySearch(SupplierLogisticsCompanySearch search) {
        Conditions condition = dao.buildConditions(search);
        try {
            return dao.count(condition);
        } catch (SQLException e) {
            log.error(e.getMessage(), e);
        }
        return 0L;
    }

    public SupplierLogisticsCompany getById(String id){
        if(id != null){
            try {
                return dao.getById(id);
            } catch (SQLException e) {
                log.error(e.getMessage(),e);
            }
        }
        return null;
    }

    public SupplierLogisticsCompany getDefLogisticsCompany(){
        Conditions conditions = new Conditions(SupplierLogisticsCompany.class);
        conditions.and().is("state", SupplierLogisticsCompanyEnum.State.Default.index());
        try {
            return  dao.get(conditions);
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return null;
    }

    public boolean add(SupplierLogisticsCompany logisticsCompany,Integer logisticsMoney) throws LogicException {
        SupplierLogisticsCompany defEntity = getDefLogisticsCompany();
        if (defEntity != null) {
            if (SupplierLogisticsCompanyEnum.State.Default.index().equals(logisticsCompany.getState())) {
                throw new LogicException("已存在默认的物流公司");
            }
        } else {
            logisticsCompany.setState(SupplierLogisticsCompanyEnum.State.Default.index());
        }
        logisticsCompany.setCreateDate(new Date());

        JdbcSession.begin();
        try {
            if (dao.insert(logisticsCompany) > 0) {
                SupplierLogisticsCostConfig costConfig = new SupplierLogisticsCostConfig();
                costConfig.setCompanyId(logisticsCompany.getId());
                costConfig.setAreaScope(ELogisticsAreaScope.Country.index());
                costConfig.setMoney(logisticsMoney);
                if (SupplierLogisticsCostConfigService.self().add(costConfig)) {
                    JdbcSession.commit();
                    return true;
                }
            }
        } catch (SQLException e) {
            log.error(e.getMessage(), e);
        } finally {
            JdbcSession.end();
        }
        return false;
    }

    public boolean updateNotNull(SupplierLogisticsCompany logisticsCompany,Integer logisticsMoney) throws LogicException {
        SupplierLogisticsCompany defEntity = getDefLogisticsCompany();
        if(defEntity != null){
            if(defEntity.getId().equals(logisticsCompany.getId())){
                logisticsCompany.setState(null);
            }else if(SupplierLogisticsCompanyEnum.State.Default.index().equals(logisticsCompany.getState())){
                throw new LogicException("已存在默认的物流公司");
            }
        }else {
            logisticsCompany.setState(SupplierLogisticsCompanyEnum.State.Default.index());
        }

        int costFlag = 0;
        SupplierLogisticsCostConfig costConfig = SupplierLogisticsCostConfigService.self().getCountryCost(logisticsCompany.getId());
        if(costConfig == null){
            costFlag = 1;   //新增
        }else if(!costConfig.getMoney().equals(logisticsMoney)){
            costFlag = 2;   //修改
        }

        JdbcSession.begin();
        try {
           if(dao.updateNotNull(logisticsCompany) > 0){
               if(costFlag == 1){
                   costConfig = new SupplierLogisticsCostConfig();
                   costConfig.setCompanyId(logisticsCompany.getId());
                   costConfig.setAreaScope(ELogisticsAreaScope.Country.index());
                   costConfig.setMoney(logisticsMoney);
                   if (SupplierLogisticsCostConfigService.self().add(costConfig)) {
                       JdbcSession.commit();
                       return true;
                   }
               }else if(costFlag == 2){
                   costConfig.setMoney(logisticsMoney);
                   if (SupplierLogisticsCostConfigService.self().updateNotNull(costConfig)) {
                       JdbcSession.commit();
                       return true;
                   }
               }else {
                   JdbcSession.commit();
                   return true;
               }
           }
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        } finally {
            JdbcSession.end();
        }
        return false;
    }

    public boolean deleteById(String id) throws LogicException {
        if(id != null) {
            JdbcSession.begin();
            try {
                if(dao.deleteById(id) > 0){
                    if(SupplierLogisticsCostConfigService.self().deleteByCompanyId(id)){
                        JdbcSession.commit();
                        return true;
                    }
                }
            } catch (SQLException e) {
                log.error(e.getMessage(),e);
            }finally {
                JdbcSession.end();
            }
        }
        return false;
    }
}
